package crm.view.desktop;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JDesktopPane;
import javax.swing.JInternalFrame;

import crm.model.teacherPC.CRMListener;
import crm.model.teacherPC.Manager;
import crm.model.teacherPC.net.StudentPC;
import crm.view.desktop.iFrame.StudentFrame;





/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class StudentDesktopPane extends JDesktopPane implements CRMListener, MouseListener {

	Manager manager;

	public StudentDesktopPane(Manager manager) {
		manager.addListener(this);
		addMouseListener(this);
		this.manager=manager;
		for (int i = 0; i < manager.getStudentCount(); i++) {
			StudentPC student = manager.getStudent(i);
			add(new StudentFrame(student, manager));
		}
	}
	public void newOrder(){
		JInternalFrame iframe[] = getAllFrames();
		int x=0,y=0,width=getWidth(),fSize=200;
		for (int i = 0; i < iframe.length; i++) {
			JInternalFrame iFr = iframe[i];
			if (x*fSize > width){
				y++;
				x=0;
			}else {
				iFr.setLocation(x*fSize, y*fSize);
				iFr.setSize(fSize, fSize);
				x++;
			}
		}
	}
	public void showAll(){
		JInternalFrame iframe[] = getAllFrames();
		for (int i = 0; i < iframe.length; i++) {
			iframe[i].setVisible(true);
		}
	}
	public void refresh(){
		removeAll();
		repaint();
		for (int i = 0; i < manager.getStudentCount(); i++) {
			StudentPC student = manager.getStudent(i);
			add(new StudentFrame(student, manager));
		}
	}


	@Override
	public void mouseClicked(MouseEvent ev) {
		if (ev.getButton() == MouseEvent.BUTTON3){
			crm.view.popup.DesktopPopup.show(this, this, ev.getX(), ev.getY());
		}
	}
	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}
	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}
	@Override
	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}
	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}
	@Override
	public void exitCRM() {


	}
	@Override
	public void studentAdded(StudentPC student) {
		add(new StudentFrame(student, manager));

	}
	@Override
	public void studentRemoved(StudentPC student) {
		refresh();

	}
	@Override
	public void actualisingComplete(StudentPC student) {

	}
	@Override
	public void connectionClosed() {
		removeAll();

	}
	@Override
	public void connectionStarted() {
		refresh();
	}

}
